package io.babystar.modules.business.controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

import io.babystar.common.utils.ExcelUtil;
import io.babystar.modules.sys.controller.AbstractController;
import io.babystar.modules.sys.entity.SysUserEntity;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.babystar.modules.business.entity.SignEntity;
import io.babystar.modules.business.service.SignService;
import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.R;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author jeson
 * @email 574197743@qq.com
 * @date 2021-03-23 20:03:33
 */
@RestController
@RequestMapping("business/sign")
public class SignController extends AbstractController {
    @Autowired
    private SignService signService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:sign:list")
    public R list(@RequestParam Map<String, Object> params){
        SysUserEntity user = this.getUser();
        params.put("organizeid",user.getBusinessId());
        PageUtils page = signService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("business:sign:info")
    public R info(@PathVariable("id") Long id){
		SignEntity sign = signService.getById(id);

        return R.ok().put("sign", sign);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("business:sign:save")
    public R save(@RequestBody SignEntity sign){
		signService.save(sign);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @RequiresPermissions("business:sign:update")
    public R update(@RequestBody SignEntity sign){
		signService.updateById(sign);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("business:sign:delete")
    public R delete(@RequestBody Long[] ids){
        signService.delete(Arrays.asList(ids));
        return R.ok();
    }

    @RequestMapping("/export")
    public void Export(@RequestParam Map<String, Object> params, HttpServletResponse response, HttpServletRequest request) throws IOException {
        //解决跨域
        response.setHeader("Access-Control-Allow-Origin", "*");
        SysUserEntity user = this.getUser();
        params.put("page","1");
        params.put("limit","50000");
        params.put("organizeid",user.getBusinessId());
        PageUtils page = signService.queryPage(params);

        //得到所有要导出的数据
        List<SignEntity> listEntity = (List<SignEntity>)page.getList();
        List<Map<String, Object>> list = this.listConvert(listEntity);
        //定义导出的excel名字
        String excelName = "打卡详情表";

        //获取需要转出的excel表头的map字段
        LinkedHashMap<String, String> fieldMap = new LinkedHashMap<>();
        fieldMap.put("id","编号");
        fieldMap.put("courseName","课程");
        fieldMap.put("studentName","学生");
        fieldMap.put("signTime","打卡时间");
        fieldMap.put("teacherName","打卡老师");
        fieldMap.put("surplus","剩余节数");
        fieldMap.put("zjTeacherName","打卡助教");
        fieldMap.put("zjSignTime","助教打卡时间");

        //导出用户相关信息
        ExcelUtil.export(list,fieldMap,excelName,response);
    }

    public <T> List<Map<String, Object>> listConvert(List<T> list) {
        // 定义List<Map<String, Object>>数组<br>　　　　　　　　　　
        // list为外部传进来的list集合
        List<Map<String, Object>> list_map = new ArrayList<Map<String, Object>>();
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(item ->{
                Map<String, Object> map = null;
                try {
                    map = (Map<String, Object>) PropertyUtils.describe(item);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
                list_map.add(map);
            });
        }
        return list_map;
    }

    /**
     * 助教打卡
     */
    @PostMapping("/zjSign")
    @RequiresPermissions("business:sign:zjSign")
    public R zjSign(@RequestBody SignEntity signEntity){
        signEntity.setZjSignTime(new Date());
        signService.updateById(signEntity);
        return R.ok();
    }

    /**
     * 查询教练提成
     */
    @GetMapping("/queryTeacherCommission")
    @RequiresPermissions("business:sign:queryTeacherCommission")
    public R queryTeacherCommission(@RequestParam Map<String, Object> params){
        SysUserEntity user = this.getUser();
        params.put("businessId",user.getBusinessId());
        List<Map<String,Object>> list = signService.getTichengBy(params);
        return R.ok().put("list", list);
    }

}
